﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.IO;
using System.Xml;

namespace SyCODE.Blog
{
    /// <summary>
    /// ConstDataProvider 的摘要说明
    /// </summary>
    public class ConstDataProvider
    {
        private ConstDataNode root;

        private void AddNodes(ConstDataNode parent, XmlNodeList nodes)
        {
            for (int i = 0; i < nodes.Count; i++)
            {
                ConstDataNode node = new ConstDataNode(nodes[i].Attributes["name"].Value);

                string key = nodes[i].Attributes["id"].Value;
                parent.AddNode(key, node);

                this.AddNodes(node, nodes[i].ChildNodes);
            }
        }

        public ConstDataProvider()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //

            this.root = new ConstDataNode("root");

            string path = Path.Combine(AppPath.GetAppRootPath(), "const.xml");

            XmlDocument constDom = new XmlDocument();
            constDom.Load(path);

            XmlNodeList nodes = constDom.DocumentElement.ChildNodes;
            for (int i = 0; i < nodes.Count; i++)
            {
                ConstDataNode node = new ConstDataNode(null);
                this.root.AddNode(nodes[i].Attributes["id"].Value, node);
                this.AddNodes(node, nodes[i].ChildNodes);
            }
        }

        public ConstDataNode this[string id]
        {
            get
            {
                return (ConstDataNode)this.root[id];
            }
        }
    }
}